package com.example.memo1.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Filter;
import android.widget.ListView;
import android.widget.SearchView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.memo1.R;
import com.example.memo1.adapter.RecordAdapter;
import com.example.memo1.adapter.RecordFilter;
import com.example.memo1.bean.Record;
import com.example.memo1.dao.AdminDao;

import java.util.ArrayList;
import java.util.List;

public class FindActivity extends AppCompatActivity {
    private ListView mListView;
    RecordAdapter adapter;
    List<Record> mRecordList;
    private Button first;
    private Button find;
    private Button mine;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find);

        mListView=findViewById(R.id.list_view);
        mRecordList=new ArrayList<>();//全局列表

        // 创建备忘录记录适配器
        adapter = new RecordAdapter(this, mRecordList);
        // 创建备忘录记录过滤器
        RecordFilter recordFilter = new RecordFilter();
        // 将备忘录记录过滤器设置为备忘录记录适配器的过滤器
       //adapter.getFilter().filter("");




        SearchView searchView = findViewById(R.id.search_view);
        searchView.setQueryHint("搜索");
       searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
           @Override
           public boolean onQueryTextSubmit(String s) {
               return false;
           }

           @Override
           public boolean onQueryTextChange(String newText) {
               String title = newText.trim();
               String content = newText.trim();


               List<Record> searchResults = new AdminDao().searchRecord("", title, content);
               adapter.setRecordList(searchResults);
               // 将搜索结果设置到适配器中

               mListView.setAdapter(adapter);
               if (searchResults.isEmpty()) {
                   AlertDialog.Builder builder = new AlertDialog.Builder(FindActivity.this);
                   builder.setMessage("未找到符合条件的记录").show();
               }

               return true;
           }
       });

        first=findViewById(R.id.first);
        first.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 =new Intent(FindActivity.this, MainActivity.class);
                startActivity(intent1);

            }
        });
        find=findViewById(R.id.find);
        find.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent2 =new Intent(FindActivity.this, FindActivity.class);
                startActivity(intent2);

            }
        });
        mine=findViewById(R.id.mine);
        mine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent3 =new Intent(FindActivity.this, MineActivity.class);
                startActivity(intent3);

            }
        });



    }


}
